
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 07/28/2010 19:56:50
-- Generated from EDMX file: G:\Personal\MotorcycleStore\MotorcycleStoreData.EF\MotorcycleStore.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MotorcycleStore];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ManufacturerMotorcycle]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Motorcycles] DROP CONSTRAINT [FK_ManufacturerMotorcycle];
GO
IF OBJECT_ID(N'[dbo].[FK_CategoryMotorcycle_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CategoryMotorcycle] DROP CONSTRAINT [FK_CategoryMotorcycle_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_CategoryMotorcycle_Motorcycle]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CategoryMotorcycle] DROP CONSTRAINT [FK_CategoryMotorcycle_Motorcycle];
GO
IF OBJECT_ID(N'[dbo].[FK_MotorcyclePhoto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Photos] DROP CONSTRAINT [FK_MotorcyclePhoto];
GO
IF OBJECT_ID(N'[dbo].[FK_MotorcyclePhoto1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Photos] DROP CONSTRAINT [FK_MotorcyclePhoto1];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Motorcycles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Motorcycles];
GO
IF OBJECT_ID(N'[dbo].[Manufacturers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Manufacturers];
GO
IF OBJECT_ID(N'[dbo].[Categories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories];
GO
IF OBJECT_ID(N'[dbo].[Photos]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Photos];
GO
IF OBJECT_ID(N'[dbo].[CategoryMotorcycle]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CategoryMotorcycle];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Motorcycles'
CREATE TABLE [dbo].[Motorcycles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(32)  NOT NULL,
    [Year] nchar(4)  NOT NULL,
    [Odometer] int  NOT NULL,
    [ManufacturerId] int  NOT NULL
);
GO

-- Creating table 'Manufacturers'
CREATE TABLE [dbo].[Manufacturers] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(32)  NOT NULL,
    [Website] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(32)  NOT NULL,
    [Description] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'Photos'
CREATE TABLE [dbo].[Photos] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PhotoData] varbinary(max)  NOT NULL,
    [ContentType] nvarchar(32)  NOT NULL,
    [Caption] nvarchar(128)  NOT NULL,
    [Height] smallint  NOT NULL,
    [Width] smallint  NOT NULL,
    [MotorcyclePhoto_Photo_Id] int  NOT NULL,
    [MotorcyclePhoto1_Photo_Id] int  NULL
);
GO

-- Creating table 'CategoryMotorcycle'
CREATE TABLE [dbo].[CategoryMotorcycle] (
    [Categories_Id] int  NOT NULL,
    [Motorcycles_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Motorcycles'
ALTER TABLE [dbo].[Motorcycles]
ADD CONSTRAINT [PK_Motorcycles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Manufacturers'
ALTER TABLE [dbo].[Manufacturers]
ADD CONSTRAINT [PK_Manufacturers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Photos'
ALTER TABLE [dbo].[Photos]
ADD CONSTRAINT [PK_Photos]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Categories_Id], [Motorcycles_Id] in table 'CategoryMotorcycle'
ALTER TABLE [dbo].[CategoryMotorcycle]
ADD CONSTRAINT [PK_CategoryMotorcycle]
    PRIMARY KEY NONCLUSTERED ([Categories_Id], [Motorcycles_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Categories_Id] in table 'CategoryMotorcycle'
ALTER TABLE [dbo].[CategoryMotorcycle]
ADD CONSTRAINT [FK_CategoryMotorcycle_Category]
    FOREIGN KEY ([Categories_Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Motorcycles_Id] in table 'CategoryMotorcycle'
ALTER TABLE [dbo].[CategoryMotorcycle]
ADD CONSTRAINT [FK_CategoryMotorcycle_Motorcycle]
    FOREIGN KEY ([Motorcycles_Id])
    REFERENCES [dbo].[Motorcycles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CategoryMotorcycle_Motorcycle'
CREATE INDEX [IX_FK_CategoryMotorcycle_Motorcycle]
ON [dbo].[CategoryMotorcycle]
    ([Motorcycles_Id]);
GO

-- Creating foreign key on [MotorcyclePhoto_Photo_Id] in table 'Photos'
ALTER TABLE [dbo].[Photos]
ADD CONSTRAINT [FK_MotorcyclePhoto]
    FOREIGN KEY ([MotorcyclePhoto_Photo_Id])
    REFERENCES [dbo].[Motorcycles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MotorcyclePhoto'
CREATE INDEX [IX_FK_MotorcyclePhoto]
ON [dbo].[Photos]
    ([MotorcyclePhoto_Photo_Id]);
GO

-- Creating foreign key on [MotorcyclePhoto1_Photo_Id] in table 'Photos'
ALTER TABLE [dbo].[Photos]
ADD CONSTRAINT [FK_MotorcyclePhoto1]
    FOREIGN KEY ([MotorcyclePhoto1_Photo_Id])
    REFERENCES [dbo].[Motorcycles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MotorcyclePhoto1'
CREATE INDEX [IX_FK_MotorcyclePhoto1]
ON [dbo].[Photos]
    ([MotorcyclePhoto1_Photo_Id]);
GO

-- Creating foreign key on [ManufacturerId] in table 'Motorcycles'
ALTER TABLE [dbo].[Motorcycles]
ADD CONSTRAINT [FK_MotorcycleManufacturer]
    FOREIGN KEY ([ManufacturerId])
    REFERENCES [dbo].[Manufacturers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MotorcycleManufacturer'
CREATE INDEX [IX_FK_MotorcycleManufacturer]
ON [dbo].[Motorcycles]
    ([ManufacturerId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------